package com.ruitu.mall_98_seller.mvp.presenter;

import com.ruitu.arad.Arad;
import com.ruitu.arad.api.BaseResult;
import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.mvp.model.UserModel;
import com.ruitu.mall_98_seller.ui.user.LoginActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.User;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class UserPresenter {

    public static class LoginP extends BasePresenter<LoginActivity, UserModel.LoginM> {

        public Disposable login(final String uName, String pwd) {
            return m.login(uName, pwd).subscribe(new Consumer<BaseResult<User>>() {
                @Override
                public void accept(BaseResult<User> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        AppHolder.instance().setUser(result.getData());
                        v.onReqComplete(1, true, result.getData());
                        Arad.bus.post(new EventModel(19090553));// 通知连接融云
                        Arad.bus.post(new EventModel(19090653));// 通知我的页面更新数据
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    ToastUtils.showShortSafe("请求异常,请稍后重试");
                }
            });
        }
    }
}
